// 检查localstorage是否存在token
let token = localStorage.getItem('token');
let time = getTime();
let token_time = localStorage.getItem('token_time');

// //存在token且token未过期
if(token && token_time && time < (parseInt(token_time)+2*60*60*1000)){//token存在且未过期，不用刷新
    //设置请求头
    $.ajaxSetup({headers: {'We-Token': token}});
    //发送请求再次检查验证这个账号是否关注了公众号,如果未关注跳转到关注页
    checkAttention();
    //验证成功继续操作
    afterVerify();
}else if(getQueryVariable('code')){//没有storage但有code，请求存token
    $('#index_detail').hide();
    $.post(baseUrl+'client/token',{
        code:getQueryVariable('code')
    },function(result){
        if(result.code === 0){
            //登录成功
            console.log(result.data);
            //设置请求头
            $.ajaxSetup({headers: {'We-Token': result.data.token}});
            //保存用户信息到storage
            let now = getTime();
            localStorage.setItem('token_time',now.toString());
            localStorage.setItem('token',result.data.token);
            $('#index_detail').show();
            afterVerify();
        }else if(result.code === 1){
            // alert(result.msg);
            location.replace("./start.html");
        }else{
            localStorage.removeItem('token');
            localStorage.removeItem('token_time');
            alert(result.msg);
        }
    },'json');
}else{//如果没有code，跳转重新登录
    localStorage.removeItem('token');
    localStorage.removeItem('token_time');
    // 重定向
    let pathname = window.location.pathname;
    pathname = pathname.substring(pathname.lastIndexOf('/') + 1, pathname.length);
    let tmpUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+TREASURE_APP_ID
        +'&redirect_uri='+PROTOCOL+'%3a%2f%2f'+SINGLE_URL+'%2ffront%2f'+pathname+'&response_type=code&scope=snsapi_base#wechat_redirect';
    location.replace(tmpUrl);
}
